package com.rc.evcharger.service.elec;

import com.rc.evcharger.common.utils.DateHelper;
import com.rc.evcharger.mapper.elec.ElecPileMapper;
import com.rc.evcharger.mapper.elec.ElecPileOfflineMapper;
import com.rc.evcharger.model.elec.ElecPile;
import com.rc.evcharger.model.elec.ElecPileOffline;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by sven on 2019/9/26
 */
@Service
public class ElecPileOfflineServiceImpl implements ElecPileOfflineService {
    @Autowired
    private ElecPileOfflineMapper elecPileOfflineMapper;

    @Autowired
    private ElecPileMapper elecPileMapper;

    @Override
    public ElecPileOfflineMapper getMapper() {
        return elecPileOfflineMapper;
    }

    @Override
    public void addPileOffline(Long pileId, Date beginTime, Date endTime, Integer loginStatus) {
        try {
            if (beginTime == null) {
                return;
            }
            ElecPile elecPile = elecPileMapper.selectByPrimaryKey(pileId);

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(beginTime);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 1);

            //得到离线时间差的天数
            int between = DateHelper.daysBetween(beginTime, endTime);

            //如果跨天
            if (between > 0) {
                for (int i = 0; i <= between; i++) {
                    //添加第一条记录
                    if (i == 0) {
                        calendar.set(Calendar.HOUR_OF_DAY, 23);
                        calendar.set(Calendar.MINUTE, 59);
                        calendar.set(Calendar.SECOND, 59);

                        ElecPileOffline pileOnline = new ElecPileOffline(pileId, beginTime, calendar.getTime(), loginStatus);
                        pileOnline.setElecStationId(elecPile.getElecStationId()); //站点ID
                        elecPileOfflineMapper.insertSelective(pileOnline);

                        calendar.add(Calendar.DATE, 1);
                        continue;
                    }
                    //添加最后一条记录
                    if (i == between && i != 0) {
                        calendar.setTime(endTime);
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 1);

                        ElecPileOffline pileOnline = new ElecPileOffline(pileId, calendar.getTime(), endTime, loginStatus);
                        pileOnline.setElecStationId(elecPile.getElecStationId()); //站点ID
                        elecPileOfflineMapper.insertSelective(pileOnline);

                        continue;
                    }

                    //添加中间数据 都是以天为单位
                    calendar.set(Calendar.HOUR_OF_DAY, 0);
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 1);
                    Date beginTime0 = calendar.getTime();

                    calendar.add(Calendar.DATE, 1);
                    calendar.set(Calendar.HOUR_OF_DAY, 23);
                    calendar.set(Calendar.MINUTE, 59);
                    calendar.set(Calendar.SECOND, 59);
                    Date endTime0 = calendar.getTime();

                    ElecPileOffline pileOnline = new ElecPileOffline(pileId, beginTime0, endTime0, loginStatus);
                    pileOnline.setElecStationId(elecPile.getElecStationId()); //站点ID
                    elecPileOfflineMapper.insertSelective(pileOnline);
                }
                //同一天内
            } else {
                ElecPileOffline pileOnline = new ElecPileOffline(pileId, beginTime, endTime, loginStatus);
                pileOnline.setElecStationId(elecPile.getElecStationId()); //站点ID
                elecPileOfflineMapper.insertSelective(pileOnline);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
